package cn.edu.suda.catalina.dataupdate.domain;

import cn.edu.suda.catalina.dataupdate.entity.Users;
import com.google.gson.Gson;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.repository.CrudRepository;

/**
 *
 * @author admin
 * @date 2017/5/23
 */
public interface UsersRepository extends CrudRepository<Users, Long> {
    Gson gson = new Gson();

    @Override
    Users save(Users user);

    /**
     * @param stringRedisTemplate
     * @param key
     * @param user
     */
    default void add(StringRedisTemplate stringRedisTemplate, String key, Users user) {
        stringRedisTemplate.opsForValue().set(key, gson.toJson(user));
    }

    default Users getUserByKey(StringRedisTemplate stringRedisTemplate, String key) {
        return gson.fromJson(stringRedisTemplate.opsForValue().get(key), Users.class);
    }
}
